package com.changjiang.myproductsale.myproduct.dao.impl;

import com.changjiang.myproductsale.myproduct.dao.BaseDao;
import com.changjiang.myproductsale.myproduct.dao.UserDao;
import com.changjiang.myproductsale.myproduct.entity.User;

import java.sql.ResultSet;
import java.util.List;

public class UserImpl extends BaseDao implements UserDao {
    @Override
    public boolean save(User user) {
        /**
         * select max(user_id)  from `user`
         *
         * insert into `user` values (16,'测试名称','123456','123456','13980840732',null,null)
         */
        try {
            //找当前最大的id值是多少
            String sql = "select max(user_id)  from `user` ";
            ResultSet set = executeQuery(sql);
            set.next();
            int maxid = set.getInt(1) + 1;
            //执行插入操作
            sql = "insert into `user` values (?,?,?,?,?,null,null)";
            int result = 0;
            result = executeUpdate(sql, maxid, user.getUser_name(), user.getUser_password(),
                    user.getUser_password(), user.getUser_number());
            if (result > 0)
                return true;
        }catch(Exception ex){
            ex.printStackTrace();
        }

        return false;
    }
    @Override
    public boolean delete(int id) {
        String sql="delete from `user`  where user_id=?";
        int result = 0;
        try {
            result = executeUpdate(sql,id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(result>0)
            return true;

        return false;
    }

    @Override
    public boolean update(User user) {
        return false;
    }

    @Override
    public User queryById(int id) {
        return null;
    }

    @Override
    public List<User> queryAll() {
        return null;
    }

    @Override
    public User login(String uname, String upwd) {
        try{
            String sql="select count(*) from `user` where user_name=? and login_password=? ";
            ResultSet set = executeQuery(sql,uname,upwd);
            set.next();
            int result = set.getInt(1);
            if(result>0){
               return this.queryByName(uname);
            }else{
                return null;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User queryByName(String uname) {
        String sql="select * from `user` where user_name=?";
        try {
            ResultSet set = executeQuery(sql, uname);
            set.next();
            int userid = set.getInt(1);
            String username = set.getString(2);
            String loginpwd = set.getString(3);
            String paypwd = set.getString(4);
            String phone = set.getString(5);
            String photo = set.getString(6);
            String code = set.getString(7);
            User user = new User(userid, username, loginpwd, paypwd, phone, photo, code);
            return user;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
